﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Clothes.Items.ModuleData;
using ApplicationLibrary;

namespace Clothes.Items.Libraries
{
    public partial class BreadCrumbsCategories : ApplicationLibrary.ExtendedControls.BreadCrumbs
    {
        DUCategories duCategories = null;
        public string Description { get; private set; }

        public event EventHandler DescriptionChanged;

        public BreadCrumbsCategories()
        {
            InitializeComponent();
        }

        private void BreadCrumbsCategories_BreadClick(object sender, BreadCrumbsEventArgs e)
        {
            CategoriesTreeForm cTree = new CategoriesTreeForm();
            cTree.Code = e.Key;

            if (cTree.ShowDialog(GlobalInfo.MainForm) == DialogResult.OK)
                Text = cTree.Code;
        }

        protected override void OnTextChanged(EventArgs e)
        {
            if (DesignMode)
                return;
            if (duCategories == null)
                duCategories = new DUCategories();

            SuspendLayout();
            Clear();
            if (DataBindings["Text"] != null)
                DataBindings["Text"].WriteValue();

            string[] catArray = Text.Split('-');
            string desc = "";
            string oldDesc = Description;

            foreach (string cat in catArray)
            {
                duCategories.Find(cat);
                if (!duCategories.GetValue<string>(CL_Categories.Description).IsEmpty())
                    desc = duCategories.GetValue<string>(CL_Categories.Description);

                AddCrumb(cat, duCategories.GetValue<string>(CL_Categories.Description));
            }
            Description = desc;

            if (DescriptionChanged != null && oldDesc != Description)
                DescriptionChanged(this, EventArgs.Empty);

            ResumeLayout();
        }
    }
}
